import 'dart:io';

import 'package:cgvc/basis/basis_provider_widget.dart';
import 'package:cgvc/generated/l10n.dart';
import 'package:cgvc/util/router_manager.dart';
import 'package:cgvc/view_model/locale_view_model.dart';
import 'package:cgvc/view_model/theme_view_model.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:oktoast/oktoast.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SpUtil.getInstance();
  LogUtil.init(tag: 'cgvc', isDebug: true, maxLen: 1024);
  runApp(MyApp());
}

//全局的导航key
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return OKToast(child: MaterialAppPage());
  }
}

class MaterialAppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MaterialAppPageState();
  }
}

class _MaterialAppPageState extends State<MaterialAppPage>
    with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return BasisProviderWidget2<ThemeViewModel, LocaleViewModel>(
        model1: ThemeViewModel(),
        model2: LocaleViewModel(),
        builder: (context, theme, locale, child) => MaterialApp(
              ///后台管理器看到的名称
              // title: 'cgvc',
              onGenerateTitle: (context) {
                return S.of(context).appName;
              },

              ///全局获取context
              navigatorKey: navigatorKey,

              ///主题
              theme: theme.themeData(),

              ///深色主题
              darkTheme: theme.themeData(platformDarkMode: true),

              debugShowCheckedModeBanner: false,

              ///启动后选择多语言
              locale: locale.locale,

              localizationsDelegates: [
                //只是实现了Material,Cupertino和基础组件的国际化
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                S.delegate
              ],
              supportedLocales: S.delegate.supportedLocales,

              //配置路由
              onGenerateRoute: RouterManager.generateRoute,
              home: SplashPage(),
            ));
  }

  @override
  void initState() {
    super.initState();

    //添加用于前后台切换的监听
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    LogUtil.v('didChangeAppLifecycleState:$state', tag: 'MaterialAppPage');

    //应用前台-重新设置状态栏-主要针对暗黑模式有底部导航栏的情况
    if (state == AppLifecycleState.resumed) {
      ThemeViewModel.setSystemBarTheme();
    }
  }
}

///启动闪屏叶
class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SplashPageState();
  }
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();

    //延迟1.5秒跳转道主页面
    Future.delayed(Duration(milliseconds: 1500), () {
      Navigator.of(context).pushReplacementNamed(RouterName.tab);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).cardColor,
      child: Column(
        //一列
        //横轴居中
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Expanded(
            flex: 1,
            child: SizedBox(),
          ),
          Image.asset(
            'assets/images/ic_logo_round.webp',
            width: 96,
            height: 96,
          ),
          SizedBox(
            height: 10,
          ),
          Image.asset(
            'assets/images/ic_slogan.webp',
            width: 205,
            height: 205 * 140 / 815,
            color: Theme.of(context).textTheme.title.color,
          ),
          SizedBox(
            height: 160,
          ),
          Expanded(
            flex: 1,
            child: SizedBox(),
          ),
          SafeArea(
            top: Platform.isIOS,
            bottom: Platform.isIOS,
            left: Platform.isIOS,
            right: Platform.isIOS,
            child: Image.asset(
              'assets/images/ic_powered.webp',
              width: 110,
              height: 110 * 100 / 436,
              color: Theme.of(context).textTheme.headline6.color,
            ),
          ),
          SizedBox(
            height: 10,
          )
        ],
      ),
    );
  }
}
